<template>
  <div class="app-container">
    <!-- 分页查询 -->
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-suffix=":"
      label-width="150px"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item
            :label="$t('locationTypeLang.locationTypeCode')"
            prop="locationTypeCode"
          >
            <el-input
              v-model="queryParams.locationTypeCode"
              :placeholder="$t('locationTypeLang.locationTypeCode')"
              maxlength="50"
              show-word-limit
              clearable
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item
            :label="$t('locationTypeLang.locationTypeName')"
            prop="locationTypeName"
          >
            <el-input
              v-model="queryParams.locationTypeName"
              :placeholder="$t('locationTypeLang.locationTypeName')"
              maxlength="50"
              show-word-limit
              clearable
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depot')" prop="depotId">
            <el-select
              v-model="queryParams.depotId"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotOptionList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item
            :label="$t('locationTypeLang.locationTypeStatus')"
            prop="locationTypeStatus"
          >
            <el-select
              v-model="queryParams.locationTypeStatus"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotStatusOptionList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 操作栏 -->
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleAdd"
        >
          {{ $t("add") }}
        </el-button>
      </el-col>
    </el-row>
    <!-- 数据列表 -->
    <el-table ref="table" v-loading="loading" :data="data" border class="mt-10">
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="80"
      />
      <el-table-column
        :label="$t('locationTypeLang.locationTypeCodeAndName')"
        prop="locationTypeCodeAndName"
        align="center"
        min-width="160"
      />
      <el-table-column
        :label="$t('locationTypeLang.locationTypeStatus')"
        align="center"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-tag :type="locationTypeStatusTag[scope.row.locationTypeStatus]">
            {{
              $t(
                getTitleByValue(
                  depotStatusOptionList,
                  scope.row.locationTypeStatus
                )
              )
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('locationTypeLang.locationLength')"
        prop="locationLength"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('locationTypeLang.locationWidth')"
        prop="locationWidth"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('locationTypeLang.locationHeight')"
        prop="locationHeight"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('locationTypeLang.locationVolume')"
        prop="locationVolume"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('locationTypeLang.depotCodeAndName')"
        prop="depotCodeAndName"
        align="center"
        min-width="160"
      />
      <el-table-column
        :label="$t('remark')"
        prop="remark"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('gmtModified')"
        prop="gmtModified"
        align="center"
        min-width="140"
      />
      <el-table-column
        :label="$t('operation')"
        width="300"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">
            {{ $t("edit") }}
          </el-button>
          <el-button
            v-if="
              scope.row.locationTypeStatus ===
                depotStatusDict.DRAFT_STATUS.value
            "
            type="text"
            @click="handleDelete(scope.row)"
          >
            {{ $t("delete") }}
          </el-button>
          <el-button
            v-if="
              scope.row.locationTypeStatus !==
                depotStatusDict.ENABLE_STATUS.value
            "
            type="text"
            @click="handleEnable(scope.row)"
          >
            {{ $t("enable") }}
          </el-button>
          <el-button
            v-if="
              scope.row.locationTypeStatus ===
                depotStatusDict.ENABLE_STATUS.value
            "
            type="text"
            @click="handleDisable(scope.row)"
          >
            {{ $t("disable") }}
          </el-button>
          <el-button type="text">{{ $t("operationLog") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 表单 -->
    <info-form
      v-model="infoParams.locationTypeId"
      :visible="infoVisiable"
      :operation="infoParams.operation"
      :title="infoTitle"
      @close="infoClose"
    />
  </div>
</template>

<script>
import { depotStatusDict } from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import {
  getDepotLocationTypePageList,
  deleteDepotLocationType,
  setDepotLocationTypeStatusEnable,
  setDepotLocationTypeStatusDisable
} from '@/api/business/depotManage/locationType'
import { getDepotListAllStatus } from '@/api/business/depotManage/depot'
import InfoForm from './components/info-form.vue'

export default {
  name: 'LocationType',
  components: {
    InfoForm
  },
  data() {
    return {
      depotOptionList: [],
      depotStatusOptionList: getDictOptionList(depotStatusDict),
      depotStatusDict: depotStatusDict,
      getTitleByValue: getTitleByValue,

      locationTypeStatusTag: ['info', 'success', 'danger'],

      queryParams: {
        pageSize: 10,
        pageNo: 1,
        locationTypeCode: undefined,
        locationTypeName: undefined,
        depotId: undefined,
        locationTypeStatus: undefined
      },
      data: [],
      total: 0,
      loading: true,
      infoParams: {
        locationTypeId: undefined,
        operation: undefined
      },
      infoVisiable: false,
      infoTitle: undefined
    }
  },
  created() {
    this.getDepotOptionList()
    this.getList()
  },
  methods: {
    async getDepotOptionList() {
      const res = await getDepotListAllStatus()
      if (!res) return
      this.depotOptionList = res.data || []
    },
    async getList() {
      this.loading = true
      const res = await getDepotLocationTypePageList({
        ...this.queryParams
      })
      this.loading = false
      if (!res) return
      this.data = res.data.resultList || []
      this.total = res.data.totalRowNum
    },
    pageNoChange(newPageNo) {
      this.queryParams.pageNo = newPageNo
      this.getList()
    },
    indexAdd(index) {
      // console.log(index)
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    },
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    handleReset() {
      this.$refs['queryParams'].resetFields()
      this.getList()
    },
    handleOpenInfo(row) {
      this.infoParams.operation = 0
      this.infoParams.locationTypeId = row.locationTypeId
      this.infoTitle = this.$t('info')
      setTimeout(() => {
        this.infoVisiable = true
      }, 400)
    },
    handleAdd() {
      this.infoParams.operation = 1
      this.infoTitle = this.$t('add')
      this.infoVisiable = true
    },
    handleEdit(row) {
      this.infoParams.operation = 2
      this.infoParams.locationTypeId = row.locationTypeId
      this.infoTitle = this.$t('edit')
      setTimeout(() => {
        this.infoVisiable = true
      }, 400)
    },
    async handleDelete(row) {
      const flag = await this.$confirm(
        this.$t('delete') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await deleteDepotLocationType({
        locationTypeId: row.locationTypeId
      })
      if (!res) return
      this.$message({
        message: this.$t('handleSuccess'),
        type: 'success'
      })
      this.getList()
    },
    async handleEnable(row) {
      const flag = await this.$confirm(
        this.$t('enable') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await setDepotLocationTypeStatusEnable({
        locationTypeId: row.locationTypeId
      })
      if (!res) return
      this.$message({
        message: this.$t('handleSuccess'),
        type: 'success'
      })
      this.getList()
    },
    async handleDisable(row) {
      const flag = await this.$confirm(
        this.$t('disable') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await setDepotLocationTypeStatusDisable({
        locationTypeId: row.locationTypeId
      })
      if (!res) return
      this.$message({
        message: this.$t('handleSuccess'),
        type: 'success'
      })
      this.getList()
    },
    infoClose() {
      this.infoVisiable = false
      this.getList()
      setTimeout(() => {
        this.infoParams.locationTypeId = undefined
        this.infoParams.operation = 0
      }, 200)
    }
  }
}
</script>

<style></style>
